package com.sunshine.ssm.controller.menuTree;

import com.sunshine.ssm.entity.MenuTree;
import com.sunshine.ssm.service.IMenuTreeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Description: 菜单树控制层
 *
 * @author hong.wei
 *         Date 2016年9月7日 下午2:47:50
 * @version V1.0
 */
@Controller
@RequestMapping("menuTree")
public class MenuTreeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(MenuTreeController.class);

    @Resource
    private IMenuTreeService service;


    @RequestMapping(value = "selectMenuTrees")
    @ResponseBody
    public Object selectMenuTrees(String id) {
        List<MenuTree> list = new ArrayList<>();
        if (StringUtils.isEmpty(id)) {
            try {
                SQL selectSql = new SQL() {{
                    SELECT("id,pid,text,state,url");
                    FROM("menu_tree");
                    WHERE("pid is null");
                }};
                list = service.selectListSql(selectSql);
            } catch (Exception e) {
                LOGGER.error("MenuTreeController.selectMenuTrees() error:" + e);
            }
        } else {
            SQL sql = new SQL() {{
                SELECT("id,pid,text,state,url");
                FROM("menu_tree");
                WHERE("pid = ?");
            }};
            list = service.selectListSql(sql, id);
        }
        return list;
    }
}
